<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>爬虫学习11——抓取让网站的图片 | XiaoZiYan</title><meta name="keywords" content="爬虫,python,bs4模块"><meta name="author" content="XiaoZiYan"><meta name="copyright" content="XiaoZiYan"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="爬虫学习python爬虫bs4模块抓取图片">
<meta property="og:type" content="article">
<meta property="og:title" content="爬虫学习11——抓取让网站的图片">
<meta property="og:url" content="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2020/10/09/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A011%E2%80%94%E2%80%94%E6%8A%93%E5%8F%96%E8%AE%A9%E7%BD%91%E7%AB%99%E7%9A%84%E5%9B%BE%E7%89%87/index.html">
<meta property="og:site_name" content="XiaoZiYan">
<meta property="og:description" content="爬虫学习python爬虫bs4模块抓取图片">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiaoziyan.top/images/307ad9a844ec8a7033ab281e7eb5585b.jpeg">
<meta property="article:published_time" content="2020-10-09T07:12:02.000Z">
<meta property="article:modified_time" content="2022-11-03T10:37:24.720Z">
<meta property="article:author" content="XiaoZiYan">
<meta property="article:tag" content="爬虫">
<meta property="article:tag" content="python">
<meta property="article:tag" content="bs4模块">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiaoziyan.top/images/307ad9a844ec8a7033ab281e7eb5585b.jpeg"><link rel="shortcut icon" href="/xiaoziyan/img/favicon.png"><link rel="canonical" href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2020/10/09/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A011%E2%80%94%E2%80%94%E6%8A%93%E5%8F%96%E8%AE%A9%E7%BD%91%E7%AB%99%E7%9A%84%E5%9B%BE%E7%89%87/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/xiaoziyan/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/xiaoziyan/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: XiaoZiYan","link":"链接: ","source":"来源: XiaoZiYan","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '爬虫学习11——抓取让网站的图片',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-03 18:37:24'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="./css/custom.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/xiaoziyan/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/archives/"><div class="headline">文章</div><div class="length-num">33</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/tags/"><div class="headline">标签</div><div class="length-num">41</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/categories/"><div class="headline">分类</div><div class="length-num">39</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://xiaoziyan.top/images/307ad9a844ec8a7033ab281e7eb5585b.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/xiaoziyan/">XiaoZiYan</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">爬虫学习11——抓取让网站的图片</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-10-09T07:12:02.000Z" title="发表于 2020-10-09 15:12:02">2020-10-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-03T10:37:24.720Z" title="更新于 2022-11-03 18:37:24">2022-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/">爬虫学习</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/">python</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/">爬虫</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/bs4%E6%A8%A1%E5%9D%97/">bs4模块</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/bs4%E6%A8%A1%E5%9D%97/%E6%8A%93%E5%8F%96%E5%9B%BE%E7%89%87/">抓取图片</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="爬虫学习11——抓取让网站的图片"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="案例——抓取图片"><a href="#案例——抓取图片" class="headerlink" title="案例——抓取图片"></a>案例——抓取图片</h2><p>为了视频和⽂档能够正常投放在市⾯上, 本课程抓取的图⽚都是唯美桌⾯系, 该⽹站还有很多让你难以⼊眠的优秀图⽚资源, 各位. 看着办吧。</p>
<p><a target="_blank" rel="noopener" href="https://www.umei.cc/bizhitupian/weimeibizhi/">https://www.umei.cc/bizhitupian/weimeibizhi/</a></p>
<p><img src="http://xiaoziyan.top/images/image-20211006110324833.png" alt="目标网站"></p>
<p>注意我选中的这个区域, 我们想要的图⽚就在这⾥. 但是, 绝对不是现在你看到的样⼦. 为什么呢? 不够⾼清⼤图~<br>真正的⾼清⼤图在⼦⻚⾯中, ⽐如, 我点击第⼀个图⽚</p>
<p><img src="http://xiaoziyan.top/images/image-20211006110406440.png" alt="大图显示"></p>
<p>这才是我想要的⼤图<del>也就是说, 我需要在⽹站的⾸⻚中, 找到⼦⻚⾯的链接, 然后请求到⼦⻚⾯, 才能看到这张⼤图</del> 不明⽩的, 把上⾯的内容重新梳理⼀下!!!!!!!</p>
<h3 id="也就是说-想要下载该⽹站图⽚-⾼清⼤图-需要三步"><a href="#也就是说-想要下载该⽹站图⽚-⾼清⼤图-需要三步" class="headerlink" title="也就是说, 想要下载该⽹站图⽚(⾼清⼤图), 需要三步,"></a>也就是说, 想要下载该⽹站图⽚(⾼清⼤图), 需要三步,</h3><ul>
<li>第⼀步, 在主⻚⾯中拿到每⼀个图⽚的⼦⻚⾯链接</li>
<li>第⼆步, 在⼦⻚⾯中找到真正的图⽚下载地址</li>
<li>第三步, 下载图⽚</li>
</ul>
<p>⼀个⼀个⼲!</p>
<h4 id="拿到⼦⻚⾯链接"><a href="#拿到⼦⻚⾯链接" class="headerlink" title="拿到⼦⻚⾯链接"></a>拿到⼦⻚⾯链接</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main_page</span>():</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;child_page_href.txt&quot;</span>, mode=<span class="string">&quot;w&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">	<span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">56</span>):</span><br><span class="line">		<span class="keyword">try</span>:</span><br><span class="line">			resp = requests.get(<span class="string">f&quot;https://www.umei.cc/bizhitupian/weimeibizhi/<span class="subst">&#123;i&#125;</span>.htm&quot;</span>)</span><br><span class="line"><span class="comment"># &lt;meta http-equiv=&quot;Content-Type&quot;content=&quot;text/html; charset=utf-8&quot; /&gt;</span></span><br><span class="line">			resp.encoding = <span class="string">&#x27;utf-8&#x27;</span> <span class="comment"># 处理中⽂乱码, 这⾥要和⻚⾯上的charset对应</span></span><br><span class="line">			main_page = BeautifulSoup(resp.text, <span class="string">&quot;html.parser&quot;</span>)</span><br><span class="line">			typeListDiv = main_page.find(<span class="string">&quot;div&quot;</span>, attrs=&#123;<span class="string">&quot;class&quot;</span>:<span class="string">&quot;TypeList&quot;</span>&#125;)</span><br><span class="line">			main_a_list =typeListDiv.find_all(<span class="string">&quot;a&quot;</span>)	</span><br><span class="line">            <span class="keyword">for</span> main_a <span class="keyword">in</span> main_a_list:</span><br><span class="line">				href = main_a.get(<span class="string">&quot;href&quot;</span>) <span class="comment">#拿到某⼀个标签中xxx属性的值</span></span><br><span class="line">                f.write(href)</span><br><span class="line">                f.write(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;https://www.umei.cc/bizhitupian/weimeibizhi/<span class="subst">&#123;i&#125;</span>.htm, 成功了!&quot;</span>)</span><br><span class="line">			time.sleep(<span class="number">0.5</span>)</span><br><span class="line">		<span class="keyword">except</span>:</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;https://www.umei.cc/bizhitupian/weimeibizhi/<span class="subst">&#123;i&#125;</span>.htm, 出错了&quot;</span>)</span><br><span class="line">			<span class="built_in">print</span>(resp.text)</span><br><span class="line">			<span class="keyword">break</span> <span class="comment"># 也可以记录下来, 供以后查错⽤</span></span><br></pre></td></tr></table></figure>

<h3 id="获取到⼦⻚⾯信息-找到下载图⽚的图⽚路径"><a href="#获取到⼦⻚⾯信息-找到下载图⽚的图⽚路径" class="headerlink" title="获取到⼦⻚⾯信息, 找到下载图⽚的图⽚路径"></a>获取到⼦⻚⾯信息, 找到下载图⽚的图⽚路径</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">child_page</span>():</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;child_page_href.txt&quot;</span>, mode=<span class="string">&quot;r&quot;</span>) asf:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">        line = line.strip()</span><br><span class="line">        resp = requests.get(line)</span><br><span class="line">        resp.encoding=<span class="string">&#x27;utf-8&#x27;</span></span><br><span class="line">        child = BeautifulSoup(resp.text,<span class="string">&quot;html.parser&quot;</span>)</span><br><span class="line">        div = child.find(<span class="string">&quot;div&quot;</span>,class_=<span class="string">&quot;ImageBody&quot;</span>)</span><br><span class="line">        img = div.find(<span class="string">&quot;img&quot;</span>)</span><br><span class="line">		<span class="keyword">if</span> img:</span><br><span class="line">            src = img.get(<span class="string">&quot;src&quot;</span>)</span><br><span class="line">            <span class="built_in">print</span>(src)</span><br><span class="line">		<span class="keyword">else</span>:</span><br><span class="line">			<span class="built_in">print</span>(line, <span class="string">&quot;没有图⽚&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="下载图⽚"><a href="#下载图⽚" class="headerlink" title="下载图⽚"></a>下载图⽚</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">download_img</span>(<span class="params">src</span>):</span></span><br><span class="line">	<span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;img/&quot;</span>+src.split(<span class="string">&quot;/&quot;</span>)[-<span class="number">1</span>],mode=<span class="string">&quot;wb&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        resp = requests.get(src) <span class="comment"># 下载图⽚</span></span><br><span class="line">        f.write(resp.content) <span class="comment"># 图⽚写⼊⽂件</span></span><br><span class="line">        <span class="built_in">print</span>(src, <span class="string">&quot;down!&quot;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="最后运⾏⼀下-看看结果"><a href="#最后运⾏⼀下-看看结果" class="headerlink" title="最后运⾏⼀下, 看看结果"></a>最后运⾏⼀下, 看看结果</h3><p>对了⼀定记着把img⽂件夹设置为excluded, 否则你的pycharm会奇卡⽆⽐</p>
<h2 id="附件（完整代码）"><a href="#附件（完整代码）" class="headerlink" title="附件（完整代码）"></a>附件（完整代码）</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1.拿到主页面的源代码. 然后提取到子页面的链接地址, href</span></span><br><span class="line"><span class="comment"># 2.通过href拿到子页面的内容. 从子页面中找到图片的下载地址 img -&gt; src</span></span><br><span class="line"><span class="comment"># 3.下载图片</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">url = <span class="string">&quot;https://www.umei.cc/bizhitupian/weimeibizhi/&quot;</span></span><br><span class="line">resp = requests.get(url)</span><br><span class="line">resp.encoding = <span class="string">&#x27;utf-8&#x27;</span>  <span class="comment"># 处理乱码</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># print(resp.text)</span></span><br><span class="line"><span class="comment"># 把源代码交给bs</span></span><br><span class="line">main_page = BeautifulSoup(resp.text, <span class="string">&quot;html.parser&quot;</span>)</span><br><span class="line">alist = main_page.find(<span class="string">&quot;div&quot;</span>, class_=<span class="string">&quot;TypeList&quot;</span>).find_all(<span class="string">&quot;a&quot;</span>)</span><br><span class="line"><span class="comment"># print(alist)</span></span><br><span class="line"><span class="keyword">for</span> a <span class="keyword">in</span> alist:</span><br><span class="line">    href = a.get(<span class="string">&#x27;href&#x27;</span>)  <span class="comment"># 直接通过get就可以拿到属性的值</span></span><br><span class="line">    <span class="comment"># 拿到子页面的源代码</span></span><br><span class="line">    child_page_resp = requests.get(href)</span><br><span class="line">    child_page_resp.encoding = <span class="string">&#x27;utf-8&#x27;</span></span><br><span class="line">    <span class="built_in">print</span>(child_page_resp)</span><br><span class="line">    child_page_text = child_page_resp.text</span><br><span class="line">    <span class="comment"># 从子页面中拿到图片的下载路径</span></span><br><span class="line">    child_page = BeautifulSoup(child_page_text, <span class="string">&quot;html.parser&quot;</span>)</span><br><span class="line">    p = child_page.find(<span class="string">&quot;p&quot;</span>, align=<span class="string">&quot;center&quot;</span>)</span><br><span class="line">    img = p.find(<span class="string">&quot;img&quot;</span>)</span><br><span class="line">    src = img.get(<span class="string">&quot;src&quot;</span>)</span><br><span class="line">    <span class="comment"># 下载图片</span></span><br><span class="line">    img_resp = requests.get(src)</span><br><span class="line">    <span class="comment"># img_resp.content  # 这里拿到的是字节</span></span><br><span class="line">    img_name = src.split(<span class="string">&quot;/&quot;</span>)[-<span class="number">1</span>]  <span class="comment"># 拿到url中的最后一个/以后的内容</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;img/&quot;</span>+img_name, mode=<span class="string">&quot;wb&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        f.write(img_resp.content)  <span class="comment"># 图片内容写入文件</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;over!!!&quot;</span>, img_name)</span><br><span class="line">    time.sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;all over!!!&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">XiaoZiYan</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://baidu.com">https://baidu.com</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan" target="_blank">XiaoZiYan</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/xiaoziyan/tags/%E7%88%AC%E8%99%AB/">爬虫</a><a class="post-meta__tags" href="/xiaoziyan/tags/python/">python</a><a class="post-meta__tags" href="/xiaoziyan/tags/bs4%E6%A8%A1%E5%9D%97/">bs4模块</a></div><div class="post_share"><div class="social-share" data-image="http://xiaoziyan.top/images/307ad9a844ec8a7033ab281e7eb5585b.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/xiaoziyan/2020/10/10/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A012%E2%80%94%E2%80%94xpath%E8%A7%A3%E6%9E%90/"><img class="prev-cover" src="http://xiaoziyan.top/images/cc1cc1d6040605cf45eccb95dd6487e1.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">爬虫学习12——xpath解析</div></div></a></div><div class="next-post pull-right"><a href="/xiaoziyan/2020/10/08/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A010%E2%80%94%E2%80%94bs4%E8%A7%A3%E6%9E%90-bs4%E6%A8%A1%E5%9D%97%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/"><img class="next-cover" src="http://xiaoziyan.top/images/ace93511bb47122a46513a0cffce442a.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">爬虫学习10——bs4解析-bs4模块安装和使用</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/xiaoziyan/2020/10/07/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A009%E2%80%94%E2%80%94bs4%E8%A7%A3%E6%9E%90-HTML%E8%AF%AD%E6%B3%95/" title="爬虫学习09——bs4解析-HTML语法"><img class="cover" src="http://xiaoziyan.top/images/d9e428449c4bad8f74848c43d771f2ef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-07</div><div class="title">爬虫学习09——bs4解析-HTML语法</div></div></a></div><div><a href="/xiaoziyan/2020/10/08/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A010%E2%80%94%E2%80%94bs4%E8%A7%A3%E6%9E%90-bs4%E6%A8%A1%E5%9D%97%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/" title="爬虫学习10——bs4解析-bs4模块安装和使用"><img class="cover" src="http://xiaoziyan.top/images/ace93511bb47122a46513a0cffce442a.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-08</div><div class="title">爬虫学习10——bs4解析-bs4模块安装和使用</div></div></a></div><div><a href="/xiaoziyan/2020/09/30/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A002%E2%80%94%E2%80%94%E7%99%BE%E5%BA%A6%E9%A1%B5%E9%9D%A2/" title="爬虫学习02——爬整个百度页面"><img class="cover" src="http://xiaoziyan.top/images/baidu.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-09-30</div><div class="title">爬虫学习02——爬整个百度页面</div></div></a></div><div><a href="/xiaoziyan/2020/10/01/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A003%E2%80%94%E2%80%94web%E8%AF%B7%E6%B1%82%E5%85%A8%E8%BF%87%E7%A8%8B%E5%89%96%E6%9E%90/" title="爬虫学习03——web请求全过程剖析"><img class="cover" src="http://xiaoziyan.top/images/web.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-01</div><div class="title">爬虫学习03——web请求全过程剖析</div></div></a></div><div><a href="/xiaoziyan/2020/10/02/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A004%E2%80%94%E2%80%94HTTP%E5%8D%8F%E8%AE%AE/" title="爬虫学习04——HTTP协议"><img class="cover" src="http://xiaoziyan.top/images/http.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-02</div><div class="title">爬虫学习04——HTTP协议</div></div></a></div><div><a href="/xiaoziyan/2020/10/03/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A005%E2%80%94%E2%80%94requests%E6%A8%A1%E5%9D%97%E5%85%A5%E9%97%A8/" title="爬虫学习05——requests模块"><img class="cover" src="http://xiaoziyan.top/images/requests.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-03</div><div class="title">爬虫学习05——requests模块</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B%E2%80%94%E2%80%94%E6%8A%93%E5%8F%96%E5%9B%BE%E7%89%87"><span class="toc-number">1.</span> <span class="toc-text">案例——抓取图片</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B9%9F%E5%B0%B1%E6%98%AF%E8%AF%B4-%E6%83%B3%E8%A6%81%E4%B8%8B%E8%BD%BD%E8%AF%A5%E2%BD%B9%E7%AB%99%E5%9B%BE%E2%BD%9A-%E2%BE%BC%E6%B8%85%E2%BC%A4%E5%9B%BE-%E9%9C%80%E8%A6%81%E4%B8%89%E6%AD%A5"><span class="toc-number">1.1.</span> <span class="toc-text">也就是说, 想要下载该⽹站图⽚(⾼清⼤图), 需要三步,</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8B%BF%E5%88%B0%E2%BC%A6%E2%BB%9A%E2%BE%AF%E9%93%BE%E6%8E%A5"><span class="toc-number">1.1.1.</span> <span class="toc-text">拿到⼦⻚⾯链接</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%88%B0%E2%BC%A6%E2%BB%9A%E2%BE%AF%E4%BF%A1%E6%81%AF-%E6%89%BE%E5%88%B0%E4%B8%8B%E8%BD%BD%E5%9B%BE%E2%BD%9A%E7%9A%84%E5%9B%BE%E2%BD%9A%E8%B7%AF%E5%BE%84"><span class="toc-number">1.2.</span> <span class="toc-text">获取到⼦⻚⾯信息, 找到下载图⽚的图⽚路径</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E5%9B%BE%E2%BD%9A"><span class="toc-number">2.</span> <span class="toc-text">下载图⽚</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E5%90%8E%E8%BF%90%E2%BE%8F%E2%BC%80%E4%B8%8B-%E7%9C%8B%E7%9C%8B%E7%BB%93%E6%9E%9C"><span class="toc-number">2.1.</span> <span class="toc-text">最后运⾏⼀下, 看看结果</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%99%84%E4%BB%B6%EF%BC%88%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%EF%BC%89"><span class="toc-number">3.</span> <span class="toc-text">附件（完整代码）</span></a></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By XiaoZiYan</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px"target="_blank" href="https://hexo.io/"> <img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为 Hexo" alt="HEXO"></a><a style="margin-inline:5px"target="_blank"href="https://butterfly.js.org/"> <img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用 Butterfly" alt="Butterfly"></a><a style="margin-inline:5px"target="_blank" href="https://www.jsdelivr.com/"> <img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用 Jsdelivr 为静态资源提供CDN加速" alt="Jsdelivr"></a><a style="margin-inline:5px"target="_blank" href="https://github.com/"> <img src="https://img.shields.io/badge/Source-gitee-d021d6?style=flat&logo=Gitee" title="本站项目由 Gitee 托管" alt="Gitee"></a><a style="margin-inline:5px"target="_blank"href="http://creativecommons.org/licenses/by-nc-sa/4.0/"> <img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris" alt="img" title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/xiaoziyan/js/utils.js"></script><script src="/xiaoziyan/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="./js/sakura.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><!-- hexo injector body_end end --></body></html>